

#include "stdafx.h"
#include "initialize.h"
#include "xmlOperator.h"
#include "reachIndex.h"
#include "database.h"
#include "tupleSetGraph.h"
#include "fenci.h"
extern vector<database*> mydbs;
extern vector<reachIndex*> myReachIndexs;
extern tupleSetGraph myGlobalTSG;

initialize::initialize()
{

}

initialize::~initialize()
{

}
void initialize::getConfFromDB()
{
	xmlOperator xmlInstance;
	xmlInstance.parseConfig("dbList.xml");
    mydbs[0]->connect();
	for (int i=1;i<mydbs.size();i++)
	{
		mydbs[i]->getSchema();
	}
}
void initialize::getConfFromFile()
{
	xmlOperator xmlInstance;
	xmlInstance.parseConfig("dbConfig.xml");
    for (int i=0;i<mydbs.size();i++)
    {
        if(!mydbs[i]->connect())
            cout<<"Error:can't connect to database:"<<mydbs[i]->dbname<<endl;
    }
}
void initialize::initialKRIndex()
{
    int i;
    for (i=0;i<myGlobalTSG.schemaNodeNum;i++)
    {
        
        myGlobalTSG.tsgNodes[i].pRelation->buildKRIndex(mydbs[0]);
    }
}
void initialize::initialReachIndex()
{
    int i,j,leftNum,rightNum;
    

    for (i=0;i<myReachIndexs.size();i++)
    {
         string sqlCommand="select ";
         bool firstString=true;
         leftNum=rightNum=0;
         for (j=0;j<myReachIndexs[i]->pLeftRel->attrNum;j++)
         {
               if (myReachIndexs[i]->pLeftRel->attrTypes[j]==0)
               {
                   if (firstString)
                   {
                        firstString = false;
                   }
                   else sqlCommand +=" ,";
                   sqlCommand+=myReachIndexs[i]->pLeftRel->attributes[j];
                   leftNum++;
               }
           }
           for (j=0;j<myReachIndexs[i]->pRightRel->attrNum;j++)
           {
               if (myReachIndexs[i]->pRightRel->attrTypes[j]==0)
               {
                   if (firstString)
                   {
                        firstString = false;
                   }
                   else sqlCommand +=" ,";
                   sqlCommand+=myReachIndexs[i]->pRightRel->attributes[j];
                   rightNum++;
               }
           }
           sqlCommand+=" from ";
           sqlCommand+=myReachIndexs[i]->pLeftRel->relationName+"@"+myReachIndexs[i]->leftDbName+", ";
           sqlCommand+=myReachIndexs[i]->pRightRel->relationName+"@"+myReachIndexs[i]->rightDbName;
           sqlCommand+=" where ";
           sqlCommand+=myReachIndexs[i]->leftColName+"="+myReachIndexs[i]->rightColName;
           //send query
           
           fenci fenciInstance;
           vector<string> leftVector;
           vector<string> rightVector;
           try{
               
		        mydbs[0]->stat->setSQL(sqlCommand);
                cout<<sqlCommand<<endl;
		        ResultSet * rs=NULL;
		        rs=mydbs[0]->stat->executeQuery();
                if(!myReachIndexs[i]->createIndex())
                    cout<<"Error:can't create reachIndex!!!"<<endl;   
                myReachIndexs[i]->openIndex();
		        while (rs->next())
		        {
                    string leftResult,rightResult; 
                    string colValue;
                    for (j=1;j<=leftNum;j++)
                    {
                        colValue=rs->getString(j);
                        leftResult=leftResult+" "+colValue;
                    }
                    for (;j<=rightNum+leftNum;j++)
                    {
                        colValue=rs->getString(j);
                        rightResult=rightResult+" "+colValue;
                    }
                    fenciInstance.getWords(leftResult.c_str(),leftVector);
                    fenciInstance.getWords(rightResult.c_str(),rightVector);
             
                    for (j=0;j<leftVector.size();j++)
                    {
                        myReachIndexs[i]->insertBatchRecord(leftVector[j],rightVector);
                    }
                    leftVector.clear();
                    rightVector.clear();
                    
		        }
                myReachIndexs[i]->saveIndex();
	       }
	       catch (SQLException excep) {
		        cerr<<excep.getErrorCode()<<","<<excep.getMessage()<<endl;
		        cout<<string(sqlCommand)<<endl;
		        getchar();
	       }
    }
}
